package controllers

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"go-cms/consts"
	"go-cms/models"
	"strconv"
)

type MenuController struct {
	BaseController
}

func (c *MenuController)Index()  {
	c.LayoutSections =make(map[string]string)
	c.LayoutSections["footerjs"] ="common/footer.html"
	c.setTpl("system/test.html")
	//c.Data["LogoSrc"]="./static/img/logo.png"
	c.Data["UserName"]="技术弯弯"
}

// Get Menu List
// HTTP /menu/list
func (c *MenuController)List()  {
	data,total := models.MenuList()

	type MenuEx struct {
		models.MenuModel
		ParentName string
	}

	var menu = make(map[int]string)
	for _,v := range data{
		menu[v.Id] = v.Name
	}

	var dataEx []MenuEx
	for _,v := range data{
		dataEx = append(dataEx,MenuEx{*v,menu[(v.Parent)]})
	}
	c.listJsonResult(consts.JRCodeSucc,"ok",total,dataEx)
}

func (c *MenuController)Edit()  {
	c.Data["id"] = c.GetString("id")
	m := models.MenuModel{}
	m.Id,_ = strconv.Atoi( c.GetString("id"))
	err :=orm.NewOrm().Read(&m)
	if err!=nil{
		logs.Info("query MenuModel err : ",err.Error())
	}
	c.jsonResult(consts.JRCodeSucc,"ok",m)
}

func (c *MenuController)EditDo()  {

}